//  Android Carpool System
//  2011.5-2011.5
//  Author Jinhuan Wang
//  name: SignupView.java
//  description: provide a sign up view 
//$history: /carpool/scr/com/carpool/SignupView.java$
package com.carpool;

import com.carpool.message.Signup;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SignupView extends Activity {
	AlertDialog.Builder adb;
	final String tag = "SignupView";
	UserInfo uinfo = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signup);
		this.adb = new AlertDialog.Builder(this);
		this.uinfo = new UserInfo(this.getApplicationContext());
		final Button signinbutton = (Button) findViewById(R.id.l_Signup);
		signinbutton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {

				String Sname = null;
				String Spassword = null;
				String SRepassword = null;
				String phone = null;
				final EditText Ename = (EditText) findViewById(R.id.UserName_edit2);
				Sname = Ename.getText().toString();
				if (Ename.getText().length() == 0) {
					AlertDialog ad = SignupView.this.adb.create();
					ad.setMessage("Please Enter a Username");
					ad.show();
					return;
				}
				final EditText Phone = (EditText) findViewById(R.id.phone_edit);
				phone = Phone.getText().toString();
				if (Phone.getText().length() != 11) {
					AlertDialog ad = SignupView.this.adb.create();
					ad.setMessage("Please Enter a Valid Phone Number");
					ad.show();
					return;
				}
				final EditText Epassword = (EditText) findViewById(R.id.PassWord_edit2);
				Spassword = Epassword.getText().toString();
				if (Epassword.getText().length() == 0) {
					AlertDialog ad = SignupView.this.adb.create();
					ad.setMessage("Please Enter a Password");
					ad.show();
					return;
				}
				final EditText ReEpassword = (EditText) findViewById(R.id.RePassWord_edit);
				SRepassword = ReEpassword.getText().toString();
				if (ReEpassword.getText().length() == 0) {
					AlertDialog ad = SignupView.this.adb.create();
					ad.setMessage("Please Enter a Password Confirm");
					ad.show();
					return;
				} else if (!SRepassword.equals(Spassword)) {
					AlertDialog ad = SignupView.this.adb.create();
					ad.setMessage("The passwords you entered do not match. Please try again");
					ad.show();
					return;

				}
				if (Signup.signup(Sname, Spassword, phone))
					
				{
					try {

						startActivity(new Intent(v.getContext(), ViewMap.class));
					} catch (Exception e) {
						Log.i(SignupView.this.tag, "Failed to Launch ViewMap ["
								+ e.getMessage() + "]");
					}
					finish();
				}
				else{
					AlertDialog ad = SignupView.this.adb.create();
					ad.setMessage("The Username you entered has already used ");
					ad.show();
					return;
					
				}

			}
		});
	}

}
